package sort;

public class ShellSort implements Sort {

    @Override
    public void sort(Comparable[] array) {
        shellSort(array, 0, array.length - 1);
    }

    public void shellSort(Comparable[] a, int lo, int hi) { // Sort a[] into increasing order.
        int N = hi - lo + 1;
        int h = lo + 1;
        while (h < N / 3) h = 3 * h + 1; // 1, 4, 13, 40, 121, 364, 1093, ...
        while (h >= 1) { // h-sort the array.
            for (int i = h; i < N; i++) { // Insert a[i] among a[i-h], a[i-2*h], a[i-3*h]... .
                for (int j = i; j >= h && SortCommon.less(a[j], a[j - h]); j -= h)
                    SortCommon.swap(a, j, j - h);
            }
            h = h / 3;
        }
    }
//
}
